import Cesium, { JulianDate } from '../Ces/Cesium'
import TimeSampleArray from './TimeSampleArray'

class TimeSystem {
  WE
  _today00: JulianDate
  _today06: JulianDate
  _today12: JulianDate
  _today18: JulianDate
  _today24: JulianDate

  static zoomTodayOutSeconds = 600
  constructor(WE) {
    this.WE = WE
  }

  get today00() {
    if (!Cesium.defined(this._today00)) {
      const currentTime = Cesium.JulianDate.now()

      this._today00 = currentTime.clone()
      this._today00.secondsOfDay = 14400 - TimeSystem.zoomTodayOutSeconds
    }

    return this._today00
  }
  get today06() {
    if (!Cesium.defined(this._today06)) {
      this._today06 = Cesium.JulianDate.addSeconds(
        this.today00,
        21600,
        new Cesium.JulianDate(),
      )
    }

    return this._today06
  }

  get today12() {
    if (!Cesium.defined(this._today12)) {
      this._today12 = Cesium.JulianDate.addSeconds(
        this.today00,
        43200,
        new Cesium.JulianDate(),
      )
    }

    return this._today12
  }

  get today18() {
    if (!Cesium.defined(this._today18)) {
      this._today18 = Cesium.JulianDate.addSeconds(
        this.today00,
        64800,
        new Cesium.JulianDate(),
      )
    }

    return this._today18
  }
  get today24() {
    if (!Cesium.defined(this._today24)) {
      this._today24 = Cesium.JulianDate.addSeconds(
        this.today00,
        86400 + TimeSystem.zoomTodayOutSeconds * 2,
        new Cesium.JulianDate(),
      )
    }

    return this._today24
  }

  setRange(start, stop, delay = TimeSystem.zoomTodayOutSeconds) {
    const viewer = this.WE.viewer
    viewer.clock.startTime =
      Cesium.JulianDate.addSeconds(start, -delay, new Cesium.JulianDate())
    viewer.clock.stopTime =
      Cesium.JulianDate.addSeconds(stop, delay, new Cesium.JulianDate())
    viewer.clock.currentTime = start.clone()
    viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP
    // viewer.clock.clockStep = Cesium.ClockStep.TICK_DEPENDENT
    if (viewer.timeline) {
      viewer.timeline.zoomTo(start, stop)
    }
    // this.playing = true
  }

  update(_frameState) {

  }

  isDestroyed() {
    return false
  }

  destroy() {
    return Cesium.destroyObject(this)
  }

  zoomToToday(startTime: JulianDate = null, stopTime: JulianDate = null) {
    const { WE } = this

    if (!Cesium.defined(startTime)) {
      startTime = this.today00
    }
    if (!Cesium.defined(stopTime)) {
      stopTime = this.today24
    }
    const currentTime = Cesium.JulianDate.now()

    WE.viewer.clock.startTime = startTime
    WE.viewer.clock.stopTime = stopTime
    WE.viewer.clock.currentTime = currentTime
    WE.viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP
    WE.viewer.clock.multiplier = 10
    if (WE.viewer.timeline) {
      WE.viewer.timeline.zoomTo(startTime, stopTime)
    }
  }

  zoomToTime(timeSampleArray) {
    if (!timeSampleArray || !timeSampleArray.start || !timeSampleArray.end) return
    const end = timeSampleArray.end.clone()
    if (timeSampleArray instanceof TimeSampleArray) {
      Cesium.JulianDate.addSeconds(end, timeSampleArray.minStepSec, end)
    }
    this.setRange(timeSampleArray.start, end)
  }

  zoomToNow() {
    const { WE } = this
    const currentTime = Cesium.JulianDate.now()

    WE.viewer.clock.startTime = this.today00
    WE.viewer.clock.stopTime = this.today24
    WE.viewer.clock.currentTime = currentTime
    WE.viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP
    WE.viewer.clock.multiplier = 1
    if (WE.viewer.timeline) {
      WE.viewer.timeline.zoomTo(this.today00, this.today24)
    }
  }
}


export default TimeSystem
